﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.AdminGroup;
using Core.AdminUser;
using Core.Authenticated;
using Core.Library;

namespace CMS.Controls.AdminUser
{
    public partial class AdminUserManager : AuthenticatedControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            var ucId = Utils.GetIdFromRequestString(Utils.UserControlId, string.Empty);

            switch (ucId)
            {
                case "edit":
                    Control c1 = LoadControl("CreateOrEdit.ascx");
                    plcControl.Controls.Add(c1);
                    break;
                case "cpw":
                    Control c2 = LoadControl("ChangePassword.ascx");
                    plcControl.Controls.Add(c2);
                    break;
                case "agu":
                    Control c3 = LoadControl("AdminGroupUser.ascx");
                    plcControl.Controls.Add(c3);
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGroup();
                BindUser();
            }
        }

        private void BindGroup()
        {
            ddlGroup.DataSource = AdminGroupController.AdminGroup_GetAll();
            ddlGroup.DataBind();
            ddlGroup.Items.Insert(0, new ListItem("-- Chọn nhóm --", "0"));
        }

        private void BindUser()
        {
            lvUser.DataSource = AdminUserController.AdminUser_GetAllByStatuses(AdminUserObject.IsSuperAdministrator + AdminUserObject.IsActived + AdminUserObject.IsSuspended);
            lvUser.DataBind();
        }

        protected string GetStatus(int status)
        {
            if (StatusHelper.IsChecked(AdminUserObject.IsSuperAdministrator, status)) // Super Administrator             
                return "<img src=\"/images/icons/dark/user2.png\" alt=\"Aministrator\" title=\"Aministrator\" class=\"topDir\" />";
            if (StatusHelper.IsChecked(AdminUserObject.IsActived, status)) // Active                
                return "<img src=\"/images/icons/dark/check.png\" alt=\"Đang hoạt động\" title=\"Đang hoạt động\" class=\"topDir\" />";

            return StatusHelper.IsChecked(AdminUserObject.IsSuspended, status) ? "<img src=\"/images/icons/dark/block.png\" alt=\"Tạm ngừng\" title=\"Tạm ngừng\" class=\"topDir\"/>" : "-"; // Tạm ngừng or unknow
        }
        
        protected void lvUser_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var hfAdminUserId       = (HiddenField)e.Item.FindControl("hfAdminUserId");
                var hfStatus            = (HiddenField)e.Item.FindControl("hfStatus");
                var hlChangePassword    = (HyperLink)e.Item.FindControl("hlChangePassword");
                var hlEdit              = (HyperLink)e.Item.FindControl("hlEdit");
                var lnkDelete           = (LinkButton)e.Item.FindControl("lnkDelete");
                             
                // Ẩn nút xóa đối với tài khoản không phải Super Administrator
                if (StatusHelper.IsChecked(ConvertUtility.ToInt32(hfStatus.Value), AdminUserObject.IsSuperAdministrator) && lnkDelete != null)                    
                    lnkDelete.Visible = false;

                // Ẩn nút sửa đối với tài khoản không phải Super Administrator
                if (StatusHelper.IsChecked(ConvertUtility.ToInt32(hfStatus.Value), AdminUserObject.IsSuperAdministrator) && lnkDelete != null)
                    hlEdit.Visible = false;
                else
                    hlEdit.NavigateUrl = UrlHelpers.EditUser(ConvertUtility.ToInt32(hfAdminUserId.Value));

                // Nếu tài khoản không phải là Super Administrator thì không hiển thị đổi mật khẩu
                if (CurrentUser.Status != AdminUserObject.IsSuperAdministrator)                
                    if (StatusHelper.IsChecked(ConvertUtility.ToInt32(hfStatus.Value), AdminUserObject.IsSuperAdministrator) && hlChangePassword != null)
                        hlChangePassword.Visible = false;                    
            }
        }

        protected void lvUser_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (string.Equals(e.CommandName, "DeleteUser"))
            {
                var dataItem = (ListViewDataItem)e.Item;
                var id = ConvertUtility.ToInt32(lvUser.DataKeys[dataItem.DisplayIndex].Value);

                if (id > 0)
                {
                    var o = AdminUserController.AdminUser_GetById(id);
                    if (o != null)
                    {
                        //o.Status = AdminUserObject.IsSuspended;                        
                        //AdminUserController.AdminUser_Update(o);
                        AdminUserController.AdminUser_Delete(o.AdminUserId);

                        ltMessage.Text = MessageUtility.ShowMessage(null, string.Format("Xóa tài khoản {0} thành công!", o.FullName), MessageType.Information);

                        BindUser();
                    }
                }
            }                        
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            var adminGroupId = ConvertUtility.ToInt32(ddlGroup.SelectedValue);
            if (adminGroupId > 0)
            {
                lvUser.DataSource = AdminUserController.AdminUser_GetAllByStatusesByAdminGroupId(adminGroupId, 
                                                AdminUserObject.IsSuperAdministrator + AdminUserObject.IsActived + AdminUserObject.IsSuspended);
                lvUser.DataBind();
            }
            else
                BindUser();
        }      
    }
}